#!/bin/bash
#
# Pulley miscellaneous (but required) functions
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 18 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


## lazy_load <function> <subdirectory> <filename>
##
## Tests to see if <function> has been loaded. If not, sources the <filename>
## in the <subdirectory> of the Pulley library directory. Returns 0 if the
## <function> is already loaded or the <filename> was sourced without error.
## Returns non-zero if an error occurs.
##
function lazy_load() {
	local status=0
	
	if [[ $# -eq 3 ]]; then
		if ! is_function $1; then
			if [[ -r "${P_LIBDIR}/$2/$3" ]]; then
				. "${P_LIBDIR}/$2/$3"
				status=$?
			else
				status=1
			fi
		fi
	else
		echo "BUG: call to lazy_load: $@"
		status=199
	fi
	
	return ${status}
}


## is_element <name> <array>
##
## Returns 0 iff <name> is an element of the <array>. Otherwise, returns 1.
##
function is_element() {
	search="$1"
	shift
	status=1
	
	for element in "$@"; do
		if [[ "${element}" == "${search}" ]]; then
			status=0
			break
		fi
	done
	return ${status}
}


## is_function <function name>
##
## Tests to see if <function name> is currently defined. Returns 0 if the
## function exists in the current scope, nonzero otherwise.
##
function is_function() {
	(declare -F | sed s/'declare -f '// | grep "$1") > /dev/null 2>&1
	return $?
}


## is_callable <name>
##
## Performs an "extended which" on <name>. Returns 0 if <name> is a currently
## defined function or if <name> refers to a command on the PATH, as reported
## by the which(1) command. Unlike which(1), does not produce any output.
##
function is_callable() {
	local status=1
	
	if is_function "${alg}"; then
		status=0
	else
		which "${alg}" > /dev/null 2>&1
		status=$?
	fi
	
	return ${status}
}


## merge_status <current status> <new status>
##
## If <current status> is zero and <new status> is non-zero, echoes
## <new status>. Otherwise, echoes <current status>. Returns 0.
##
function merge_status() {
	if [[ $1 -ne 0 || $2 -eq 0 ]]; then
		echo $1
	elif [[ $2 -ne 0 ]]; then
		echo $2
	fi
}


## split_string <delimeter> <string>
##
## Splits a string into sections based on the <delimeter>, which is removed,
## and outputs the resulting strings one per line. Returns 0 upon success,
## non-zero if an error occurs.
##
function split_string() {
	echo "$2" | awk -F "$1" '{for (i=1; i<=NF; i++) print $i}'
	return $?
}


## t_timestamp
##
## Returns a timestamp expressed in seconds since the epoch. Returns 0 on
## success, non-zero on failure.
##
function t_timestamp() {
	date +%s
	return $?
}
